package it.omnisong.client.widget;

import java.util.ArrayList;

import it.omnisong.client.BasicSearchService;
import it.omnisong.client.BasicSearchServiceAsync;
import it.omnisong.client.WikiSearchService;
import it.omnisong.client.WikiSearchServiceAsync;
import it.omnisong.client.model.StockArtist;
import it.omnisong.client.model.StockSong;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;

public class BasicSearchPanel extends HorizontalPanel{
	
	final private BasicSearchPanel self = this;
	private ResultPanel resultPanel = null; // il pannello dei risultati su cui scrivere
	private ArrayList<StockArtist> resultArtist = null;
	private ArrayList<StockSong> resultSong = null;
	
	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final WikiSearchServiceAsync wikiService = GWT
			.create(WikiSearchService.class);
	private final BasicSearchServiceAsync basicSearchService = GWT
			.create(BasicSearchService.class);
	
	public BasicSearchPanel() {
		
		//self.setBorderWidth(2);
		self.setWidth("600px");
		
		final Button sendButton = new Button("Search");
		final TextBox queryField = new TextBox();
		
		/* settaggi per il campo query */
		queryField.setText("omnisong");
		queryField.setFocus(true);
		queryField.selectAll();
		queryField.setWidth("538px");
		queryField.setHeight("16px");
		
		// We can add style names to widgets
		sendButton.addStyleName("sendButton");
		
		// aggiungo le widget al pannello search
		this.add(queryField);
		this.add(sendButton);
		
		class SearchHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				sendQueryStringToServer();
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					sendQueryStringToServer();
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a response.
			 */
			private void sendQueryStringToServer() {
				
				/* First, we validate the input. 
				errorLabel.setText("");
				String textToServer = nameField.getText();
				if (!FieldVerifier.isValidName(textToServer)) {
					errorLabel.setText("Please enter at least four characters");
					return;
				} */

				String queryString = queryField.getText();
				
				// Then, we send the input to the server.
				sendButton.setEnabled(false);
				wikiService.wikiSearch(queryString,
						new AsyncCallback<ArrayList<StockArtist>>() {
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								System.out.println(caught.toString());
							}

							public void onSuccess(ArrayList<StockArtist> result) {
								resultPanel.showArtistResult(result);
							}
						});
				basicSearchService.basicSearch(queryString,
						new AsyncCallback<ArrayList<StockSong>>() {
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								//ErrorDialogBox dialogBox = new ErrorDialogBox();
								//dialogBox.center();
								System.out.println(caught.toString());
							}

							public void onSuccess(ArrayList<StockSong> result) {

								resultPanel.showSongResult(result);
								
							}
						});
				sendButton.setEnabled(true);
			}
		}
		
		SearchHandler handler = new SearchHandler();
		queryField.addKeyUpHandler(handler);
		sendButton.addClickHandler(handler);
	}

	/**
	 * @param resultArtist the resultArtist to set
	 */
	public void setResultArtist(ArrayList<StockArtist> resultArtist) {
		this.resultArtist = resultArtist;
	}

	/**
	 * @param resultSong the resultSong to set
	 */
	public void setResultSong(ArrayList<StockSong> resultSong) {
		this.resultSong = resultSong;
	}

	public void addResultPanel(ResultPanel resultPanel) {
		this.resultPanel = resultPanel;
	}
	
	


}
